﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DAO;
using BUS;
using System.Windows.Forms;
using Common;
public partial class Admin_Pages_login : BasePage
{
    private Encrypt _objEncrypt = new Encrypt();
    BUS_WRUSER _busUser = new BUS_WRUSER();
    protected void Page_Load(object sender, EventArgs e)
    {
        txtUserName.Focus();
        if (!IsPostBack)
        {
            if (Request.Cookies["UserName"] != null && Request.Cookies["Password"] != null)
            {
                txtUserName.Text = Request.Cookies["UserName"].Value;
                txtPassword.Attributes["value"] = Request.Cookies["Password"].Value;
                if (Request.Cookies["RememberMe"] != null)
                {
                    chkRememberMe.Checked = true;
                }
            }
        }
    }
    private void RememberMe()
    {
        if (chkRememberMe.Checked == true)
        {
            Response.Cookies["UserName"].Expires = DateTime.Now.AddDays(30);
            Response.Cookies["Password"].Expires = DateTime.Now.AddDays(30);
            Response.Cookies["RememberMe"].Expires = DateTime.Now.AddDays(30);
        }
        else
        {
            Response.Cookies["UserName"].Expires = DateTime.Now.AddDays(-1);
            Response.Cookies["Password"].Expires = DateTime.Now.AddDays(-1);
            Response.Cookies["RememberMe"].Expires = DateTime.Now.AddDays(-1);

        }
        Response.Cookies["UserName"].Value = txtUserName.Text.Trim();
        Response.Cookies["Password"].Value = txtPassword.Text.Trim();
        Response.Cookies["RememberMe"].Value = chkRememberMe.Checked == true ? "true" : "false";
    }
    protected void btnCancel_Click(object sender, EventArgs e)
    {

    }
    protected void btnLogin_Click1(object sender, EventArgs e)
    {
        string id = txtUserName.Text.Trim();
        string pass = txtPassword.Text.Trim();
        string sMahoa = _objEncrypt.Mahoa(pass);
        if (sMahoa.Equals("Không tìm thấy file data.dat") == true)
        {
            lblErr.Text = "Not found data.dat";
            return;
        }
        WRUSER user = new WRUSER();
        user = _busUser._GetObject(p => p.Username == txtUserName.Text && p.Password == sMahoa && p.IDUserGroup == 1 && p.Active == "Y");
        if (user != null)
        {
            Session["Username"] = txtUserName.Text.Trim();
            Session["userid"] = user.ID;
            Response.Redirect("~/1/WRUSERManager.htm");
        }
        else
        {
            lblErr.Text = "Login failed. Please check your username or password again!";
            txtUserName.Text = "";
            txtPassword.Text = "";
            txtUserName.Focus();
        }
    }
}